<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- pid为空 -->
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!--  -->
        <el-tree :data="data" :props="defaultProps">
          <template slot-scope="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="user" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <!-- <el-col @click.native="handleNodeClick(data, node)">{{ data.manager }}</el-col> -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <!--  v-if="data.children.length === 0"  有子组件就不能删除一级组件 -->
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >
                          删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- v-if="showDialog" -->
      <!-- close 是el-dialog自带的 -->
      <el-dialog
        :title="isEdit ? '编辑部门' : '添加部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hDialogClose"
      >
        <DeptDialog
          ref="refDeptDialog"
          :origin-list="originList"
          :is-edit="isEdit"
          :pid="curId"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 引入 getDepartments Ajax
import { getDepartments, delDepartment } from '@/api/departments'
// 引入 arrayToTree 封装的树形结构
import { arrayToTree } from '@/utils/index'
//  引入子组件
import DeptDialog from './deptDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      isEdit: false, // 记录当前是否处于编辑
      curId: '', // 当前选中的id
      data: [],
      defaultProps: {
        children: 'children',
        // 修改为 name 与接口一致
        label: 'name'
      },
      showDialog: false, // 是否显示弹窗
      //
      originList: []
    }
  },
  created() {
    //  发送Ajax
    this.loadDepartment()
  },
  methods: {
    hClose() {
      this.showDialog = false
    },
    hSuccess() {
      // 子组件内部的操作成功
      // 1.关闭弹窗
      this.showDialog = false
      // 2.更新数据
      this.loadDepartment()
    },
    // 点击编辑
    hEdit(id) {
      // 1.更新当前id
      this.curId = id
      // 2.设置状态为编辑
      this.isEdit = true
      // 3. 打开弹窗
      this.showDialog = true

      // // 找到子组件 ,要求他发送请求,获取详情
      // this.$nextTick(()=>{
      //   console.log(this.$refs.deptDialog)
      //   this.$refs.deptDialog.loadDepartmentDetailById()
      // })
      this.$nextTick(() => {
        console.log(this.$refs.refDeptDialog)
        // 获取子组件的实例
        // 调用它的方法
        this.$refs.refDeptDialog.loadDepartmentDetail()
      })
    },
    // 点击添加
    hAdd(id) {
      // 1.显示弹窗
      this.showDialog = true
      // 2.拿到当前的id ,方便传给子组件
      console.log(id)
      this.curId = id
      this.isEdit = false
    },
    // 删除
    hDel(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.doDel(id)
      })
    },
    restForm() {
      this.$refs.deptForm.resetFields()
    },
    // 验证提示重置
    hDialogClose() {
      // 找到子组件,调用它的重置表单方法
      this.$refs.refDeptDialog.resetForm()
    },
    // 封装Ajax
    async loadDepartment() {
      const res = await getDepartments()
      // console.log('res',res)
      res.data.depts.shift()
      // this.data = res.data.depts
      // console.log(this.data)
      // 数组转成数
      this.data = arrayToTree(res.data.depts)
      // this.originList = res.data.depts.map(({id,pid,code,name}) => { return {id,pid,code,name}})
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code
        }
      })
    },
    async doDel(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
      this.loadDepartment()
    },
    handleNodeClick(data) {
      // console.log(data)
    }
  }
}
</script>
